package com.kingyea.mobilepolice.role.controller;

import com.kingyea.mobilepolice.orgrole.bean.OrgRoleCondition;
import com.kingyea.mobilepolice.orgrole.bean.OrgRolePo;
import com.kingyea.mobilepolice.role.bean.Role;
import com.kingyea.mobilepolice.role.bean.RoleCondition;
import com.kingyea.mobilepolice.role.bean.RolePo;
import com.kingyea.mobilepolice.role.service.RoleService;
import com.kingyea.mobilepolice.security.SecurityUtils;
import com.kingyea.mobilepolice.system.Result;
import com.kingyea.mobilepolice.system.StateCode;
import com.kingyea.mobilepolice.util.ExceptionConditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description Role 控制层
 * @date Created in 2018-03-30 11:36:12
 */
@RequestMapping(value = "/role")
@RestController
public class RoleController {

    private static final Logger LOGGER = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @PostMapping("/list")
    public Result<List<Role>> list(@RequestBody RoleCondition condition) throws Exception {
        condition.setList(roleService.search(condition));
        return new Result(condition);
    }

    /**
     * 通过USER_ID查找对应的角色集合
     *
     * @param userId
     * @return
     */
    @PostMapping(value = "/searchByuserId/{userId}")
//    @PreAuthorize("hasAuthority('search-user-role')")
    public Object detail(@PathVariable String userId) {
        return new Result<>(roleService.searchByUserId(userId));
    }

    /**
     * 通过当前用户的USER_ID对应的角色集合
     *
     * @param
     * @return
     */
    @PostMapping(value = "/myRole")
    public Object myRoles() {
        // 当前登录用户是否管理 userId 对应的用户
        return new Result<>(roleService.searchByUserId(SecurityUtils.getCurrentUser().getUserId()));
    }


    /**
     * 删除依赖用户的角色
     *
     * @param po
     * @return
     */
    @PostMapping("/delRolesForUser")
    @PreAuthorize("hasAuthority('delete-role-for-user')")
    public Object delRolesForUser(@RequestBody RolePo po) {
        return roleService.delRolesForUserId(po);
    }

    /**
     * 为用户添加角色依赖
     *
     * @param po
     * @return
     */
    @PreAuthorize("hasAuthority('add-role-for-user')")
    @PostMapping("/addRolesForUser")
    public Object addRolesForUser(@RequestBody RolePo po) {
        return roleService.addRolesForUserId(po);
    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PreAuthorize("hasAuthority('create-role')")
    @PostMapping("/save")
    public Object save(@RequestBody Role role) throws Exception {
        ExceptionConditions.checkNotNull(role, StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkNotNull(role.getRoleName(), StateCode.NO_ARGUMENT_ERROR);
        return roleService.save(role);
    }

    @PreAuthorize("hasAuthority('delete-role')")
    @PostMapping("/del/{id}")
    public Object del(@PathVariable("id") Integer id) throws Exception {
        return roleService.deleteById(id);
    }

    @PostMapping("/findById/{id}")
    public Object findById(@PathVariable("id") Integer id) {
        return roleService.getById(id);
    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PreAuthorize("hasAuthority('update-role')")
    @PostMapping("/update")
    public Object update(@RequestBody Role role) throws Exception {
        ExceptionConditions.checkNotNull(role, StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkNotNull(role.getRoleName(), StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkNotNull(role.getId(), StateCode.NO_ARGUMENT_ERROR);
        Role role_ = roleService.getById(role.getId());
        ExceptionConditions.checkNotNull(role_, StateCode.SERVER_DATA_MISSING_ERROR);
        role_.setRoleName(role.getRoleName());
        role_.setDescription(role.getDescription());
        role_.setSort(role.getSort());
        return roleService.update(role_);
    }

    /**
     * @Description:
     * @Param: [condition]
     * @return: java.lang.Object
     * @Author: cxc
     * @Date: 2018.04.27 20:20
     */
    @PostMapping("/org/search")
    public Object orgSearch(@RequestBody OrgRoleCondition condition) {
//        ExceptionConditions.checkArrayNotEmpty(condition.getOrgSerialNos(), StateCode.NO_ARGUMENT_ERROR);
        RoleCondition roleCondition = new RoleCondition();
        BeanUtils.copyProperties(roleCondition, condition);
        roleCondition.setList(roleService.searchByOrgSerialNo(condition));
        roleCondition.setTotal(condition.getTotal());
        return roleCondition;
    }

    @PostMapping("/org/update")
    public Object orgUpdate(@RequestBody OrgRolePo po) {
        ExceptionConditions.checkNotNull(po.getRoleIds(), StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkStringNotEmpty(po.getOrgSerialNo(), StateCode.NO_ARGUMENT_ERROR);
        return roleService.updateForOrg(po);
    }

}
